<?php

namespace app\admin\model;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use support\Model;

/**
 * 角色模型
 * @property int $id
 * @property string $name 角色名称
 * @property string|null $description 角色描述
 * @property int $status 状态:0=禁用,1=启用
 * @property int $sort 排序
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class Role extends Model
{
    /**
     * The table associated with the model.
     * @var string
     */
    protected $table = 'role';
    /**
     * The primary key associated with the table.
     * @var string
     */
    protected $primaryKey = 'id';
    /**
     * Indicates if the model should be timestamped.
     * @var bool
     */
    public $timestamps = true;
    /**
     * The name of the "created at" column.
     * @var string|null
     */
    const CREATED_AT = 'create_time';
    /**
     * The name of the "updated at" column.
     * @var string|null
     */
    const UPDATED_AT = 'update_time';
    /**
     * The attributes that are mass assignable.
     * @var array
     */
    protected $fillable = [
        'name',
        'description',
        'status',
        'sort',
    ];
    /**
     * The attributes that should be cast to native types.
     * @var array
     */
    protected $casts = [
        'status' => 'integer',
        'sort' => 'integer',
        'create_time' => 'datetime:Y-m-d H:i:s',
        'update_time' => 'datetime:Y-m-d H:i:s',
    ];

    /**
     * 获取角色的管理员
     */
    public function admins(): BelongsToMany
    {
        return $this->belongsToMany(Admin::class, 'admin_role', 'role_id', 'admin_id');
    }

    /**
     * 获取角色的菜单
     */
    public function menus(): BelongsToMany
    {
        return $this->belongsToMany(Menu::class, 'role_menu', 'role_id', 'menu_id');
    }
}
